package cn.waka.sys.admin.service;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import cn.waka.sys.admin.dao.AppDao;
import cn.waka.sys.admin.dao.AppLogDao;
import cn.waka.sys.admin.dao.AppVersionDao;
import cn.waka.sys.admin.object.App;
import cn.waka.sys.admin.object.AppLog;
import cn.waka.sys.admin.object.AppVersion;
import cn.waka.sys.admin.object.Page;

@Service
public class AppService {

	
	private AppDao appDao;
	
	private AppLogDao appLogDao;
	
	private AppVersionDao appVersionDao;

	@Autowired
	public void setAppDao(AppDao appDao) {
		this.appDao = appDao;
	}

	@Autowired
	public void setAppLogDao(AppLogDao appLogDao) {
		this.appLogDao = appLogDao;
	}
	
	@Autowired
	public void setAppVersionDao(AppVersionDao appVersionDao) {
		this.appVersionDao = appVersionDao;
	}

	public Page findPage(Page p){
		List<App> list=appDao.findList(p,null);
		int count=appDao.findCount(p,null);
		p.setData(list);
		p.setCount(count);
		int totalCount=1;
		if(count%p.getSize()>0){
			totalCount=(int) Math.floor(count/p.getSize());
			totalCount++;
		}else{
			totalCount=count/p.getSize();
		}
		p.setTotalPage(totalCount);
		return p;
	}
	public Page findPage(Page p,Map<String,String> param){
		List<App> list=appDao.findList(p,param);
		int count=appDao.findCount(p,param);
		p.setData(list);
		p.setCount(count);
		int totalCount=1;
		if(count%p.getSize()>0){
			totalCount=(int) Math.floor(count/p.getSize());
			totalCount++;
		}else{
			totalCount=count/p.getSize();
		}
		p.setTotalPage(totalCount);
		return p;
	}


	public App findById(int id) {
		return appDao.findById(id);
	}

	
	public boolean updateStatus(int id,int status,int userId){
		boolean r=appDao.updateStatus(id, status)>0;
		AppLog log=new AppLog();
		log.setAppId(id);
		log.setAction(status==1?3:4);
		log.setTime(new Timestamp(System.currentTimeMillis()));
		log.setUserId(userId);
		appLogDao.save(log);
		return r;
	}
	
	public int save(App app){
		if(app==null){
			return 0;
		}
		int rs=0;
		AppLog log=new AppLog();
		if(app.getId()==0){
			String appkey=DigestUtils.md5DigestAsHex(UUID.randomUUID().toString().getBytes()).toLowerCase();
			String appSecret=DigestUtils.md5DigestAsHex(UUID.randomUUID().toString().getBytes()).toLowerCase();
			app.setAppKey(appkey);
			app.setAppSecret(appSecret);
			rs=appDao.insert(app);
			app.setId(rs);
			log.setAction(AppLog.ACTION_ADD);
		}else{
			
			log.setAction(AppLog.ACTION_EDIT);
			rs=appDao.update(app);
		}
		log.setTime(new Timestamp(System.currentTimeMillis()));
		log.setAppId(app.getId());
		log.setUserId(app.getCreateUserId());
		appLogDao.save(log);
		return rs;
		
	}
	
	
	public int saveLog(AppLog log){
		return appLogDao.save(log);
	}
	
	public Page findLogPage(Page p,int appId){
		int page=p.getPage();
		int start=(page-1)*p.getSize();
		int size=p.getSize();
		List<AppLog> list=appLogDao.findPage(appId,start,size);
		int count=appLogDao.findTotal(appId);
		p.setCount(count);
		p.setData(list);
		int totalPage=0;
		 if(count>0&&count%p.getSize()>0){
			 totalPage=(int) Math.floor(count/p.getSize());
			 totalPage++;
		}else{
			totalPage=count/p.getSize();
		}
		 if(totalPage==0){
			 totalPage=1;
		 }
		p.setTotalPage(totalPage);
		
		return p;
	}
	
	public Page findVersionPage(Page p,int appId){
		int page=p.getPage();
		int start=(page-1)*p.getSize();
		int size=p.getSize();
		List<AppVersion> list=appVersionDao.findPage(appId,start,size);
		int count=appVersionDao.findTotal(appId);
		p.setCount(count);
		p.setData(list);
		
		int totalPage=0;
		if(count>0&&count%p.getSize()>0){
			 totalPage=(int) Math.floor(count/p.getSize());
			 totalPage++;
		}else{
			totalPage=count/p.getSize();
		}
		if(totalPage==0){
			totalPage=1;
		}
		p.setTotalPage(totalPage);
		
		return p;
	}
	
	public AppVersion findVersionById(int id){
		AppVersion v=appVersionDao.getById(id);
		v.setId(id);
		return v;
	}
	
	public int delVersionById(int id){
		return appVersionDao.del(id);
	}
	
	public int saveVersion(AppVersion version){
		if(version==null){
			return 0;
		}
		if(version.getId()==0){
			int r=appVersionDao.findNewVersionExist(version.getAppId(), version.getVersion());
			if(r>0){
				return -1;
			}
			return appVersionDao.insert(version);
		}
		AppVersion version1=appVersionDao.getById(version.getId());
		int r=appVersionDao.findEditVersionExist(version.getAppId(), version1.getVersion(), version.getVersion());
		if(r>0){
			return -1;
		}
		return appVersionDao.update(version);
	}
	
	public Map<String,String> getMaxVersion(int id){
		return appVersionDao.getMaxVersion(id);
	}
}
